package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 1.用户登录
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {

        //获取接收数据
        final String username1 = employee.getUsername();
        final String password1 = employee.getPassword();
        final String password = DigestUtils.md5DigestAsHex(password1.getBytes());

        //获取用户名对应信息
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username1);
        final Employee emp = employeeService.getOne(queryWrapper);

        //1.用户名检查

        if (emp == null) {
            return R.error("用户名不存在!");
        }
        //2.用户密码检查
        if (!emp.getPassword().equals(password)) {
            return R.error("用户密码错误!");
        }

        //3.用户状态检查

        if (emp.getStatus() == 0) {
            return R.error("该用户已被禁用!");
        }

        //4.登录成功并将用户id存入session中
        session.setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 2.用户退出
     * @param session
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpSession session) {
        //销毁session
        session.invalidate();
        return R.success("销毁成功！");
    }


    /**
     * 3.添加用户
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Employee employee, HttpSession session) {
        //设置初始默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //添加用户
        employeeService.save(employee);
        return R.success("员工添加成功！");
    }


    /**
     * 4.按条件分页查询员工信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {

        //设置分页参数
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //设置模糊查询条件
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        //设置按更新时间排序
        queryWrapper.orderByDesc("update_time");
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 5.根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    /**
     * 6.员工信息修改
     *（包括直接修改状态）
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        Long id = (Long) request.getSession().getAttribute("employee");
        employeeService.updateById(employee);
        return R.success("员工信息修改成功！");
    }

}
